﻿
Public Class DefaultCriterionProcessor
    Implements ICriterionProcessor

    Private Const SINGLE_SPACE As String = " "

    ''' <summary>
    ''' The criteria of the report, expressed as an sql where clause
    ''' </summary>
    Public Property SqlCriteria As String Implements ICriterionProcessor.SqlCriteria

    ''' <summary>
    ''' The criteria of the report, expressed in human terms.
    ''' </summary>
    ''' <value></value>
    ''' <returns>String</returns>
    ''' <remarks>
    ''' The string returned is [Label]:[value]<br/>
    ''' For compbo boxes, the description is returned. For all other 
    ''' controls, we return the value of the control.
    ''' </remarks>
    Public Property HumanCrieria As String Implements ICriterionProcessor.HumanCrieria

    Public Overridable Sub buildCriterion(ByVal criterion As ICriterionMetadata, _
                              ByVal rptsqlBuilder As IReportCriteriaProcessor) _
                          Implements ICriterionProcessor.buildCriterion

        Dim sql As String = String.Empty

        If Not criterion.isRange Then

            If Not criterion.hasValue Then
                System.Diagnostics.Trace.WriteLine(String.Format("Criterion: {0}: null value.", criterion.Name))
                Me.SqlCriteria = String.Empty
                Return
            End If

            sql = sql & criterion.SqlExpression.Replace("{0}", "{" & rptsqlBuilder.parameterCounter & "}")
            rptsqlBuilder.parameterCounter = rptsqlBuilder.parameterCounter + 1
            rptsqlBuilder.sqlParameterValues.Add(criterion.selectedvalue)

            Me.HumanCrieria = criterion.labelText & ":" & CStr(criterion.selectedDisplayValue)

        Else
            If Not criterion.hasValue OrElse _
                    Not criterion.hasValue2 Then

                System.Diagnostics.Trace.WriteLine(String.Format("Criterion (IsRange=1): {0}: null value.", criterion.Name))
                Me.SqlCriteria = String.Empty
                Return
            End If

            Dim sqlexpression As String = criterion.SqlExpression
            'note: first replace {1} and then {0}.  Otherwise, {0} becomes {1} and is also replaced
            'on line 36
            sqlexpression = sqlexpression.Replace("{1}", "{" & (rptsqlBuilder.parameterCounter + 1) & "}")
            sqlexpression = sqlexpression.Replace("{0}", "{" & rptsqlBuilder.parameterCounter & "}")

            'Replace("{1}", "{" & rptsqlBuilder.parameterCounter + 1 & "}")

            sql = sql & sqlexpression
            rptsqlBuilder.parameterCounter = rptsqlBuilder.parameterCounter + 2

            rptsqlBuilder.sqlParameterValues.Add(criterion.selectedvalue)
            rptsqlBuilder.sqlParameterValues.Add(criterion.selectedvalue2)

            Me.HumanCrieria = criterion.labelText & ":" & CStr(criterion.selectedDisplayValue) & _
                " " & criterion.labelText2 & ":" & CStr(criterion.selectedDisplayValue2)


        End If

        System.Diagnostics.Trace.WriteLine( _
                    String.Format("SQL From Criterion: {0}: {1}.", criterion.Name, sql))

        Me.SqlCriteria = sql
        Return

    End Sub

End Class